package cards;

import cards.Card;
import static cards.Card.Suit;
import static cards.Card.Face;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CardDeck {
  public static final int DECK_SIZE = Suit.values().length * Face.values().length;

  private List<Card> deck;

  public CardDeck() {
    this(true);
  }

  public CardDeck(boolean shuffle) {
    if (shuffle) {
      this.shuffle();
    } else {
      this.newdeck();
    }
  }

  public synchronized void newdeck() {
    deck = new ArrayList<Card>(DECK_SIZE); 

    // Create the deck.
    for (Suit suit : Suit.values()) {
      for (Face face : Face.values()) {
        deck.add(new Card(suit, face));
      }
    }
  }

  public synchronized void shuffle() {
    this.newdeck();
    Collections.shuffle(deck);
  }

  /**
   * Draw a card from the top of the deck.
   * @return Card from the top of the deck.
   * Destructive to the deck.
   */
  public synchronized Card draw() {
    return deck.remove(0);
  }

  public int size() {
    return deck.size();
  }

  public String toString() {
    return deck.toString();
  }

  public static void main(String[] args) {
    CardDeck deck = new CardDeck(false);

    System.out.println("Unshuffled:");
    System.out.println(deck);
    System.out.println();

    deck.shuffle();
    System.out.println("Shuffled:");
    System.out.println(deck);
    System.out.println();

    deck.shuffle();
    System.out.println("Shuffled:");
    System.out.println(deck);
    System.out.println();
  }
}
